/**
 * 
 */
package pl.edu.agh.to;

import java.util.List;
import java.util.Observable;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

/**
 * @author karulis
 * 
 */
public class RatesActivity extends TemplateActivity {

	private Spinner coursesSpinner;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onCreateImplementation() {
		setupCoursesSpinner();
	}

	@Override
	public void update(Observable obj, Object arg) {
		switch ((DBChange) arg) {
		case CourseChange:
			setupCoursesSpinner();
			break;
		default:
			super.update(obj, arg);
			break;
		}
	}

	@Override
	int getLayoutResID() {
		return R.layout.rates;
	}

	@Override
	int getSpinnerID() {
		return R.id.rates_participants_spinner;
	}

	@Override
	int getListViewID() {
		return R.id.rates_courses_list;
	}

	@Override
	List<Object> listViewContent() {
		return getdBAccess().getRates(getSpinnerSelectedItem(),
				getCoursesSpinnerSelectedItem());
	}

	@Override
	List<Object> spinnerContent() {
		return getdBAccess().getParticipants(getCoursesSpinnerSelectedItem());
	}

	List<Object> spinnerCoursesContent() {
		return getdBAccess().getAllCourses();
	}

	@Override
	void onSpinnerItemSelected() {
		setupList();
	}

	/**
	 * @return the spinner
	 */
	private Spinner getCoursesSpinner() {
		if (coursesSpinner == null) {
			coursesSpinner = (Spinner) findViewById(R.id.rates_courses_spinner);
		}
		return coursesSpinner;
	}

	String getCoursesSpinnerSelectedItem() {
		Object object = getCoursesSpinner().getSelectedItem();
		if (object != null) {
			return getCoursesSpinner().getSelectedItem().toString();
		}
		// TODO should be in resources
		return "";
	}

	/**
	 * 
	 */
	private void setupCoursesSpinner() {
		ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(this,
				android.R.layout.simple_spinner_item, spinnerCoursesContent());
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		getCoursesSpinner().setAdapter(adapter);
		getCoursesSpinner().setOnItemSelectedListener(
				new RatesOnItemSelectedListener(this));
		setupSpinner();
	}

	@Override
	protected int getMenuRes() {
		return R.menu.rates_menu;
	}

	@Override
	protected void scheduleChange() {
		// do nothing
	}

	@Override
	protected void attendChange() {
		setupSpinner();
	}

	@Override
	protected void rateChange() {
		setupList();
	}

	@Override
	protected void courseChange() {
		setupCoursesSpinner();
	}

	@Override
	protected void participantChange() {
		setupSpinner();
	}

}

class RateAddDialog extends RateRemoveDialog {

	RateAddDialog(TemplateActivity context) {
		super(context);
	}

	@Override
	protected void action() {
		DBAccess.instance(getActivity()).addRate(getSpinnerItem(),
				getCoursesSpinnerItem(),
				new Rate(new Double(getDialogSpinnerItem())));
	}

	@Override
	protected List<Object> getSpinnerContent() {
		return Rate.ratesList();
	}

	@Override
	protected int getMessageId() {
		return R.string.rates_add_dialog_text;
	}

	@Override
	protected int getDialogSpinnerId() {
		return R.id.add_rate_spinner;
	}

	@Override
	protected int getViewId() {
		return R.layout.add_rate_dialog;
	}

}

class RateRemoveDialog extends AddToDialog {
	private String coursesSpinnerItem;

	/**
	 * @return the coursesSpinnerItem
	 */
	public String getCoursesSpinnerItem() {
		return coursesSpinnerItem;
	}

	/**
	 * @param coursesSpinnerItem
	 *            the coursesSpinnerItem to set
	 */
	public void setCoursesSpinnerItem(String coursesSpinnerItem) {
		this.coursesSpinnerItem = coursesSpinnerItem;
	}

	RateRemoveDialog(TemplateActivity context) {
		super(context);
		setCoursesSpinnerSelectedItem();
	}

	@Override
	protected int getMessageId() {
		return R.string.rates_dialog_text;
	}

	@Override
	public void prepare() {
		super.prepare();
		setCoursesSpinnerSelectedItem();
	}

	/**
	 * 
	 */
	private void setCoursesSpinnerSelectedItem() {
		setCoursesSpinnerItem(((RatesActivity) getActivity())
				.getCoursesSpinnerSelectedItem());
	}

	@Override
	protected void action() {
		DBAccess.instance(getActivity()).removeRate(getSpinnerItem(),
				getCoursesSpinnerItem(), getRate());
	}

	Rate getRate() {
		return (Rate) getDialogSpinner().getSelectedItem();
	}

	@Override
	protected int getViewId() {
		return R.layout.remove_rate_dialog;
	}

	@Override
	protected List<Object> getSpinnerContent() {
		return DBAccess.instance(getActivity()).getRates(getSpinnerItem(),
				getCoursesSpinnerItem());
	}

	@Override
	protected int getDialogSpinnerId() {
		return R.id.remove_rate_spinner;
	}

}
